<template>
  <el-dialog title="收货单查询" :close-on-click-modal="false" :visible.sync="visible" top="-20px" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <div class="text item">
      <el-form inline size="mini">
        <el-form-item label="商品类别">
          <el-select v-model="dataForm.ctgId" clearable>
            <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName">
            <el-button slot="append" @click="openSupplyDialog">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="收货时间">
          <el-row>
            <el-col :span="11">
              <el-date-picker style="width: 100%;" v-model="dataForm.auditTimeStart" type="date" placeholder="选择日期" :picker-options="checkOptionsStart" @change="checkChangeEnd"></el-date-picker>
            </el-col>
            <el-col :span="2" style="text-align: center;">至</el-col>
            <el-col :span="11">
              <el-date-picker style="width: 100%;" v-model="dataForm.auditTimeEnd" type="date" placeholder="选择日期" :picker-options="checkOptionsEnd" @change="checkChangeStart"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider content-position="left">查询数据</el-divider>

    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <!-- <el-button icon="el-icon-check" type="primary" @click="checkItemData" size="mini">选 择</el-button> -->
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <el-table :data="dataList" ref="singleTable" border style="margin-bottom: 15px;" v-loading="dataListLoading">
      <el-table-column type="index" width="50">
        <template slot-scope="scope">
          <el-radio v-model="dataForm.billId" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;" @change="checkReceipt(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="billId" label="收货单号"></el-table-column>
      <el-table-column prop="ctgName" label="商品类型"></el-table-column>
      <el-table-column prop="supplyName" label="制造商"></el-table-column>
      <el-table-column prop="auditUserName" label="收货人"></el-table-column>
      <el-table-column prop="auditTime" label="收货时间"></el-table-column>
      <el-table-column label="操作"  >
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-download" @click="checkDetail(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗，查询制造商 -->
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <!-- 弹窗，收货单详情 -->
    <receive-form v-if="receiveVisible" ref="receiveDialog"></receive-form>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { receiveGoodsFetchList, receiveGoodsGetObj, checkFormDataById } from '@/api/eims/purchase/receive-goods'
  import { getDeptInfo } from '@/api/admin/dept'

  import { getCtgList } from '@/api/eims/ctg'
  import SearchSupply from '@/components/business/SearchSupply'
  import ReceiveForm from '@/views/eims/purchase/receive-goods/detail'

  export default {
    props: ["checkStock"],
    data() {
      return {
        visible: false,
        ctgOptions: [],
        dataForm: {},
        checkOptionsStart: {},
        checkOptionsEnd: {},
        ctgList: [],
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        supplyVisible: false,
        receiveVisible: false
      }
    },
    components:{
      SearchSupply,
      ReceiveForm
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init() {
        this.visible = true
        this.checkChangeEnd()
        this.checkChangeStart()
        this.pageIndex = 1
        for(let key in this.dataForm){
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
        this.getDataList()
        this.getGoodsCtgList()
      },
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = this.filterParams(this.dataForm)
        receiveGoodsFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            let data = response.data.data.records
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },

      checkReceipt(data) {
        let inDept = data.inDeptId
        let inStock = data.inStockId
        let list = []
        checkFormDataById(data.id,this.checkStock).then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data.detailList
            this.$emit('refreshData',resultData)
            this.visible = false
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  打开查询制造商弹窗
      openSupplyDialog() {
        let data = {ctgId: ''}
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init(data)
        })
      },
      //  获取商品类别列表
      getGoodsCtgList() {
        getCtgList().then(result => {
          this.ctgOptions = result.data.data
        })
      },
      //  获取制造商信息
      getSupplyData(data) {
        this.$set(this.dataForm,'supplyId',data.id)
        this.$set(this.dataForm,'supplyName',data.name)
      },
      checkDetail(id){
        this.receiveVisible = true
        this.$nextTick(() => {
          this.$refs.receiveDialog.init(id)
        })
      },
      checkChangeStart(){
        this.checkOptionsStart = Object.assign({},this.checkOptionsStart,{
          disabledDate: (time) => {
            if(this.dataForm.auditTimeEnd){
              return time.getTime() > this.dataForm.auditTimeEnd
            }else{
              return false
            }
          }
        })
      },
      checkChangeEnd() {
        this.checkOptionsEnd = Object.assign({},this.checkOptionsEnd,{
          disabledDate: (time) => {
            if(this.dataForm.auditTimeStart){
              return time.getTime() < this.dataForm.auditTimeStart
            }else{
              return false
            }
          }
        })
      }
    }
  }
</script>

<style>
</style>
